package cn.com.zero.common.auth.security.listener;

import cn.com.zero.common.auth.security.service.AbstractLoginAuthenticationService;
import cn.com.zero.common.auth.security.vo.UserDetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;

/**
 * @author xiongxj
 * @version 1.0.0
 * @Description 登录认证成功的事件监听器
 * @createTime 2023/3/27 9:21
 */
@Component
public class LoginSuccessEventListener implements ApplicationListener<AuthenticationSuccessEvent> {

    private AbstractLoginAuthenticationService loginAuthenticationService;

    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        UserDetailsVO userDetailsVO = (UserDetailsVO) event.getAuthentication().getPrincipal();

        // 登录成功监听器处理。登录成功后，重置 失败次数为 0，添加登录成功日志等
        loginAuthenticationService.loginSuccessListenerProcess(userDetailsVO.getUserInfo());
    }

    @Autowired
    public void setLoginAuthenticationService(AbstractLoginAuthenticationService loginAuthenticationService) {
        this.loginAuthenticationService = loginAuthenticationService;
    }
}
